package org.jeecg.modules.flower.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.model.JsonFormat;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flower.entity.*;
import org.jeecg.modules.flower.mapper.FlowerCostMapper;
import org.jeecg.modules.flower.mapper.FlowerStorehouseMapper;
import org.jeecg.modules.flower.service.*;
import org.jeecg.modules.flowermater.entity.FlowerMaterialCost;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 蔬菜成本和统计
 * @Author: jeecg-boot
 * @Date: 2022-06-29
 * @Version: V1.0
 */
@Service
@Log4j2
public class FlowerCostServiceImpl extends ServiceImpl<FlowerCostMapper, FlowerCost> implements IFlowerCostService {

    @Autowired
    private FlowerStorehouseMapper flowerStorehouseMapper;
    @Autowired
    private ISysDictService sysDictService;
    /**
     *
     * 收入查询
     * @param page
     * @param storehouse
     * @param cost
     * @param sDate
     * @param eDate
     * @return
     */
    @Override
    public IPage<FlowerCost> queryCosts(IPage<FlowerCost> page, String storehouse, String cost, String sDate, String eDate) {
        if(StringUtils.isNotBlank(eDate)){
            eDate = eDate+" 23:59:59";
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        String psnId = sysUser.getId();
        IPage<FlowerCost> flowerCostIPage = baseMapper.queryCosts(page, storehouse, cost, sDate, eDate, psnId);
        return flowerCostIPage;
    }

    /**
     *   蔬菜明细表查询统计===盘点 上期结余。本期入库，出库， 本期结余  -->
     * @param sDate
     * @param eDate
     * @return
     */
    @Override
    public JSONObject flowerAccount( String storehouse, String cost2,String sDate, String eDate) {
        QueryWrapper<FlowerStorehouse> queryWrapperHouse =new QueryWrapper<>();
        List<FlowerStorehouse> flowerStorehousesList = flowerStorehouseMapper.selectList(queryWrapperHouse) ;
        Map<String,String> housemap =new HashMap<>();
        for(FlowerStorehouse flowerStorehouse : flowerStorehousesList){
            housemap.put(flowerStorehouse.getCode(), flowerStorehouse.getName());
        }
        List<DictModel> ch_cost = sysDictService.getDictItems("ch_cost");
        Map<String,String> costMap =new HashMap<>();
        for(DictModel cost : ch_cost){
            costMap.put(cost.getValue(), cost.getText());
        }
        Map<String , FlowerAcont> map =new HashMap<>();
        //入库
        List<FlowerAcont> huaBenQiRuKuLists = baseMapper.huaBenQiRuKu(  storehouse,   cost2,   sDate,   eDate);
        //出库
        List<FlowerAcont> huaBenQiChuKuList = baseMapper.huaBenQiChuKu( storehouse,   cost2,  sDate,   eDate);
        //上期结余
        List<FlowerAcont> huaSINList = baseMapper.huaSIN(storehouse,   cost2,   sDate,   eDate);
        List<FlowerAcont> huaSOutList = baseMapper.huaSOut(storehouse,   cost2,   sDate,   eDate);

        Double zero = Double.valueOf("0");
        Double ru = zero;
        Double chu = zero;
        Double sru = zero;
        Double schu = zero;
        for(FlowerAcont huaBenQiRuKu : huaBenQiRuKuLists){
            String key = huaBenQiRuKu.getCost()+ huaBenQiRuKu.getStorehouse()+huaBenQiRuKu.getVarietyCode();
            huaBenQiRuKu.setId(key);
            huaBenQiRuKu.setJieyu(zero);
            huaBenQiRuKu.setBenchuku(zero);
            huaBenQiRuKu.setBenjieyu(huaBenQiRuKu.getBenruku());
            map.put(key , huaBenQiRuKu);
            ru +=huaBenQiRuKu.getBenruku();
        }
//        log.info("总入库："+ ru);

        for(FlowerAcont huaBenQiChuKu : huaBenQiChuKuList){
            String key = huaBenQiChuKu.getCost()+ huaBenQiChuKu.getStorehouse()+huaBenQiChuKu.getVarietyCode();
            FlowerAcont flowerAcont = map.get(key);
            Double benchuku = huaBenQiChuKu.getBenchuku();
            if (flowerAcont != null) {
                Double jieyu = flowerAcont.getJieyu();
                Double benruku = flowerAcont.getBenruku();
                flowerAcont.setBenchuku( benchuku );
                flowerAcont.setBenjieyu( jieyu + benruku - benchuku );
            }else{
                huaBenQiChuKu.setId(key);
                huaBenQiChuKu.setJieyu( zero );
                huaBenQiChuKu.setBenruku( zero );
                huaBenQiChuKu.setBenjieyu( zero - benchuku );
                map.put( key , huaBenQiChuKu);
            }
            chu += benchuku;
        }
//        log.info("总出库："+chu);


        for(FlowerAcont huaSIN : huaSINList){
            String key = huaSIN.getCost()+ huaSIN.getStorehouse()+huaSIN.getVarietyCode();
            FlowerAcont flowerAcont = map.get(key);
            Double jieyu = huaSIN.getJieyu();
            if (flowerAcont != null) {
                Double benruku = flowerAcont.getBenruku();
                Double benchuku = flowerAcont.getBenchuku();
                flowerAcont.setJieyu( jieyu );
                flowerAcont.setBenjieyu(jieyu + benruku - benchuku);
            }else{
                huaSIN.setId(key);
                huaSIN.setBenruku( zero );
                huaSIN.setBenchuku( zero );
                huaSIN.setBenjieyu( jieyu );
                map.put( key , huaSIN);
            }
            sru += jieyu;
        }
//        log.info("上期入库 ：" + sru);

        for(FlowerAcont huaSOut : huaSOutList){
            String key = huaSOut.getCost()+ huaSOut.getStorehouse()+huaSOut.getVarietyCode();
            FlowerAcont flowerAcont = map.get(key);
            Double jieyu = huaSOut.getJieyu();
            if ( flowerAcont != null ) {
                Double jieyuold = flowerAcont.getJieyu();
                Double benruku = flowerAcont.getBenruku();
                Double benchuku = flowerAcont.getBenchuku();
                flowerAcont.setJieyu( jieyuold -  jieyu);
                flowerAcont.setBenjieyu( jieyuold -  jieyu + benruku - benchuku );
//                flowerAcont.setCost( costMap.get( flowerAcont.getCost() ) );
//                flowerAcont.setStorehouse( housemap.get( flowerAcont.getStorehouse() ) );
            }else{
                huaSOut.setId(key);
                huaSOut.setJieyu( zero - jieyu );
                huaSOut.setBenruku( zero );
                huaSOut.setBenchuku( zero );
                huaSOut.setBenjieyu( zero - jieyu );
                map.put( key , huaSOut);
            }
            schu += jieyu;
        }
        map.forEach((k, v) -> {
            v.setCost(costMap.get( v.getCost() ));
            v.setStorehouse(housemap.get( v.getStorehouse() ));
        });
//        log.info("上期出库 ：" + schu);
//        log.info("总上期结余：" + (sru - schu) );

        JSONObject json = new JSONObject();
        json.put("total", "上期结余："+  (sru - schu)+"  ,   本期入库："+ ru + "  ,   本期出库："+ chu+ "  ,  本期结余："+ ( sru - schu + ru - chu ));

        List<FlowerAcont> mapValueList = new ArrayList<FlowerAcont>(map.values());
        json.put("list",mapValueList);
        return json;
    }
    /**
     * 结算单汇总报表
     */
    @Override
    public IPage<FlowerOrderCountList> orderCountList(IPage<FlowerOrderCountList> page, Wrapper<FlowerOutorder> queryWrapper,String sDate,String eDate) {
        if(StringUtils.isNotBlank(eDate)){
            eDate = eDate+" 23:59:59";
        }
        return baseMapper.orderCountList(page, queryWrapper, sDate, eDate);
    }

    @Override
    public IPage<FlowerAcont> huiZonglist(IPage<FlowerAcont> page, Wrapper<FlowerAcont> queryWrapper, String sDate, String eDate) {
        if(StringUtils.isNotBlank(eDate)){
            eDate = eDate+" 23:59:59";
        }
        return baseMapper.huiZonglist(page, queryWrapper,sDate,eDate);
    }


    /**
     * 预出库结算单
     * @param page
     * @param queryWrapper
     * @param sDate
     * @param eDate
     * @return
     */
    @Override
    public IPage<FlowerOrderPreCountList> orderPreAccontlist(IPage<FlowerOrderPreCountList> page, Wrapper<FlowerOrderPreCountList> queryWrapper, String sDate, String eDate) {
        if(StringUtils.isNotBlank(eDate)){
            eDate = eDate+" 23:59:59";
        }
        return baseMapper.orderPreAccontlist(page,queryWrapper,sDate,  eDate);
    }

    /**
     *
     * 入库汇总单打印
     * @descriptionauthor
     * @author gf
     * 2022-10-27 13:20
     * [page, queryWrapper, sDate, eDate]
     * @return com.baomidou.mybatisplus.core.metadata.IPage<org.jeecg.modules.flower.entity.FlowerTagIn>
     */
    @Override
    public IPage<TFlowerTagIn2> queryInListByDate(IPage<TFlowerTagIn2> page, Wrapper<TFlowerTagIn2> queryWrapper, String sDate, String eDate) {
        IPage<TFlowerTagIn2> flowerTagInIPage = baseMapper.queryInListByDate(page, queryWrapper, sDate, eDate);
        return flowerTagInIPage;
    }
    /**
     *  实物出库汇总报表
     * @descriptionauthor
     * @author gf
     * 2022-10-28 15:12
     * [page, isPre, sDate, eDate]
     * @return com.baomidou.mybatisplus.core.metadata.IPage<org.jeecg.modules.flower.entity.FlowerOutorderHZ>
     */
    @Override
    public IPage<FlowerOutorderHZ> queryOutListByDate(IPage<FlowerOutorderHZ> page,String storehouse, String isPre, String sDate, String eDate) {
        IPage<FlowerOutorderHZ> flowerOutorderHZIPage = baseMapper.queryOutListByDate(page,  storehouse, isPre, sDate, eDate);
        return flowerOutorderHZIPage;
    }  /**
     *  实物出库 日 报表
     * @descriptionauthor
     * @author gf
     * 2022-10-28 15:12
     * [page, isPre, sDate, eDate]
     * @return com.baomidou.mybatisplus.core.metadata.IPage<org.jeecg.modules.flower.entity.FlowerOutorderHZ>
     */
    @Override
    public IPage<FlowerOutorderDayHZ> queryOutDayListByDate(IPage<FlowerOutorderDayHZ> page,String storehouse, String isPre, String sDate, String eDate) {
        IPage<FlowerOutorderDayHZ> flowerOutorderHZIPage = baseMapper.queryOutDayListByDate(page,  storehouse, isPre, sDate, eDate);
        return flowerOutorderHZIPage;
    }

    /**
     *销售系统大屏获取-销售数据
     * @descriptionauthor
     * @author gf
     * 2022-11-14 13:56
     * []
     * @return java.util.List<org.jeecg.modules.flower.entity.FlowerOutorderHZ>
     */
    @Override
    public List<FlowerOutorderHZ> getSealInfos() {
        return baseMapper.getSealInfos();
    }
    @Override
    public List<FlowerOutorderHZ> getThreeMonthSealInfos() {
        return baseMapper.getThreeMonthSealInfos();
    }


}
